// pages/orderlist/orderlist.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['待付款', '待发货','待收货','已完成'],
    currentTab:0,
    shopList: [],
    isHideShop:false,
    isHideDele:true
  },

  toDetail:function(e){
    var index = e.currentTarget.dataset.index;
     wx.navigateTo({
       url: '/pages/orderdetail/orderdetail?orId=' + this.data.shopList[index].orderid,
     })
  },
  navbarTap: function (e) {
    console.log(e.currentTarget.dataset.idx);
    wx.showLoading({
      title: '正在加载...',
    })
    if (e.currentTarget.dataset.idx==0){
      this.setData({
        isHideDele: false
      })
    }else{
      this.setData({
        isHideDele: true
      })
    }
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
    var self = this;
    var url = app.serverUrl +'About/orderDetails';
    var index = e.currentTarget.dataset.idx;
    wx.request({
      url: url,
      method: 'post',
      data: {
        openid: app.openid,
        ordertype: index.toString()
      },
      success: function (res) {
        console.log(res)
        if(res.data.data.length==0){
          self.setData({
            isHideShop:true
          })
        }else{
          self.setData({
            shopList: res.data.data,
            isHideShop: false
          })
        }
        wx.hideLoading()
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var index=options.state;
    if (index == 0) {
      this.setData({
        isHideDele: false
      })
    }
    this.setData({
      currentTab: index
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '正在加载...',
    })
    var self=this;
    var url = app.serverUrl +'About/orderDetails';
    wx.request({
      url: url,
      method:'post',
      data:{
        openid: app.openid,
        ordertype: self.data.currentTab
      },
      success:function(res){
        console.log(res);
        if (res.data.data.length == 0) {
          self.setData({
            isHideShop: true
          })
        } else {
          self.setData({
            shopList: res.data.data,
            isHideShop: false
          })
        }
        wx.hideLoading()
      }
    })

  },
  //删除
  delete:function(e){
    var self = this;
    var url = app.serverUrl + 'About/orderDelete'
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '确定删除该订单？',
      success:function(res){
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: url,
            method: 'post',
            data: {
              orderid: self.data.shopList[index].orderid
            },
            success: function (res) {
              console.log(res);
              if (res.data.flag == app.suc){
                self.data.shopList.splice(index, 1);
                self.setData({
                  shopList: self.data.shopList
                })
                wx.showToast({
                  title: '删除成功',
                })
              }
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '大易益·传统文化大舞台，爱好者的掌中宝！',
      path: '/pages/index/index'
    }
  
  }
})